﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Physis.Client.Models.Configuration {

    public class UserAccountModel : ConfigurationItemModel {
                
        #region Private Properties

        private Security.UserAccount userAccount = null;

        private List<Security.RoleMembership> roleMembership = null;

        #endregion


        #region Public Properties

        public override Int64 ConfigurationItemId { set { UserAccountId = value; } }

        public override Core.CoreConfigurationObject ConfigurationItem { get { return userAccount; } }


        public Int64 UserAccountId {

            set {

                userAccount = Application.Security.UserAccount (value);

                if (userAccount == null) {

                    userAccount = new Security.UserAccount (Application);

                }

            }

        }

        public Security.UserAccount UserAccount { get { return userAccount; } set { userAccount = value; } }


        public System.Web.Mvc.SelectList SecurityAuthoritySelectList {

            get {

                List<Security.SecurityAuthority> items;

                items = (from currentSecurityAuthority in Application.Security.SecurityAuthorities ()

                         where ((currentSecurityAuthority.Enabled = true) || (currentSecurityAuthority.Id == UserAccount.SecurityAuthorityId))

                         orderby currentSecurityAuthority.Name

                         select currentSecurityAuthority).ToList ();

                return new System.Web.Mvc.SelectList (items, "Id", "Name", UserAccount.SecurityAuthorityId);

            }

        }

        public System.Web.Mvc.SelectList EnvironmentSelectList {

            get {

                List<Enterprise.Environment> items;

                items = (from currentEnvironment in Application.Enterprise.Environments()

                         where ((currentEnvironment.Enabled = true) || (UserAccount.HasEnvironmentAccess (currentEnvironment.Id)))

                         orderby currentEnvironment.Name

                         select currentEnvironment).ToList ();

                return new System.Web.Mvc.SelectList (items, "Id", "Name");

            }

        }

        public List<Security.RoleMembership> RoleMembership {

            get {

                if (roleMembership == null) {

                    roleMembership = Application.Security.RoleMembershipByUserAccountId (UserAccount.Id);

                    roleMembership = roleMembership.OrderBy (membership => membership.RoleName).ToList ();

                }

                return roleMembership;

            }

        }

        #endregion 


        #region Constructors

        public UserAccountModel (Int64 forUserAccountId) {

            UserAccountId = forUserAccountId;

            return;

        }

        public UserAccountModel (System.Collections.Specialized.NameValueCollection form) {

            UpdateValues (form);

            return;

        }

        #endregion 


        #region Public Methods

        public override void UpdateValues (System.Collections.Specialized.NameValueCollection form) {

            // DO NOT CALL THE BASE UPDATE, IT CANNOT BE USED AS A SOURCE FOR NAME/DESCRIPTION 

            // NAME/DESCRIPTION ARE NOT FIELDS THAT ARE ALLOWED TO BE MODIFIED 

            // RELOAD BASE OBJECT FROM DATABASE 

            ConfigurationItemId = Data.Readers.FormReader.Int64FromForm (form, "ConfigurationItem.Id");

            ConfigurationItem.Enabled = Data.Readers.FormReader.BooleanFromForm (form, "ConfigurationItem.Enabled");


            if (UserAccount.Id == 0) {  // NEW ACCOUNT ONLY PROPERTIES 

                // THIS IS THE ONLY TIME THAT NAME/DESCRIPTION IS ENABLED FOR POST BACK IS NEW ACCOUNTS 

                // GRAB THE INFORMATION HERE SINCE WE ALREADY DID ALL THE WORK IN THE UI GETTING THE INFORMATION

                ConfigurationItem.Name = Data.Readers.FormReader.StringFromForm (form, "ConfigurationItem.Name");

                ConfigurationItem.Description = Data.Readers.FormReader.StringFromForm (form, "ConfigurationItem.Description");


                UserAccount.SecurityAuthorityId = Data.Readers.FormReader.Int64FromForm (form, "UserAccount.SecurityAuthorityId");

                UserAccount.DistinguishedName = Data.Readers.FormReader.StringFromForm (form, "UserAccount.DistinguishedName");

                UserAccount.DisplayName = Data.Readers.FormReader.StringFromForm (form, "UserAccount.DisplayName");

            }


            // ROLE PERMISSIONS DESERIALIZE JSON 

            UserAccount.Roles.Clear ();

            String jsonRoleMembership = Data.Readers.FormReader.StringFromForm (form, "Role.Membership");

            List<Security.RoleMembership> roleMembership = new List<Security.RoleMembership> (); // INITIALIZZE SO THAT WE CAN GET THE TYPE IN THE NEXT STATEMENT
            
            roleMembership = (List<Security.RoleMembership>)new System.Web.Script.Serialization.JavaScriptSerializer ().Deserialize (jsonRoleMembership, roleMembership.GetType ());

            foreach (Security.RoleMembership currentRoleMembership in roleMembership) {

                UserAccount.Roles.Add (Application.Security.Role (currentRoleMembership.RoleId, true));

            }

            return;

        }

        #endregion 

    }

}